----------------------------------------------------------------------------------------------------------------------------------
--[[

    台词 调取 组件

]]--
----------------------------------------------------------------------------------------------------------------------------------
local hutao_com_dialogue = Class(function(self, inst)
    self.inst = inst

    self.talking_task = {}

    self.official_string_replacer = {}

    self.cd = {}
end)

------------------------------------------------------------------------------------------------------------------------------
---
    function hutao_com_dialogue:GetInspect(index)
        return TUNING.HUTAO_FNS:GetDialogue(index)
    end
    function hutao_com_dialogue:Say(index,delay)
        -------------------------------------------------------
        --- 检查是否可以说话。
            if self.inst.components.talker == nil then
                print("ERROR: The entity has no talker component.",self.inst,index)
                return
            end
        -------------------------------------------------------
        --- 清理掉之前的任务。
            for i, v in ipairs(self.talking_task) do
                v:Cancel()
            end
            self.talking_task = {}
        -------------------------------------------------------
        --- 开始新的任务。
            if delay == nil then -- 没有延迟，直接说。
                local dialogue = self:GetInspect(index)
                if type(dialogue) == "string" then
                    self.inst.components.talker:Say(dialogue)
                elseif type(dialogue) == "table" then
                    --- 多行文本。
                    for i,v in ipairs(dialogue) do
                        if type(v) == "table" and type(v[1]) == "string" and type(v[2]) == "number" then
                            local task = self.inst:DoTaskInTime(v[2],function()
                                self.inst.components.talker:Say(v[1])
                            end)
                            table.insert(self.talking_task,task)
                        end
                    end
                end
            elseif type(delay) == "number" then
                local task self.inst:DoTaskInTime(delay,function()
                    self:Say(index)
                end)
                table.insert(self.talking_task,task)
            end
        -------------------------------------------------------
    end
    function hutao_com_dialogue:SayWithCD(index,cd,delay)
        if self.cd[index] then
            return
        end
        self.cd[index] = self.inst:DoTaskInTime(cd,function()
            self.cd[index] = nil
        end)
        self:Say(index,delay)
    end
------------------------------------------------------------------------------------------------------------------------------
--- 官方的台词替代器: 例如拒绝食物的调取路径是 GetString(inst, "ANNOUNCE_EAT", "YUCKY")
    function hutao_com_dialogue:SetStringReplacerFn(stringtype,modifier,fn)
        self.official_string_replacer[stringtype] = self.official_string_replacer[stringtype] or {}
        self.official_string_replacer[stringtype][modifier] = fn
    end
    function hutao_com_dialogue:GetString(stringtype, modifier)
        if self.official_string_replacer[stringtype] and self.official_string_replacer[stringtype][modifier] then
            return self.official_string_replacer[stringtype][modifier]()
        end
        return nil
    end
------------------------------------------------------------------------------------------------------------------------------
return hutao_com_dialogue







